import json
import pandas as pd
import numpy as np

from sqlalchemy.orm import Session
from core.response import resp_500
from core.logger import logger
from datetime import datetime, timedelta

from app.fault_self_recovery.models.fsr_container_hpa import FSRContainerHPA


class FSRContainerHpaHandler():
    """  HostDate Handler
    """
    def __init__(self):
        pass


    ##### 增删改查
    def get_container_hpa_search(self, db: Session, page: int, per_page: int, field: str, value: str):
        """
        根据表字段模糊匹配，获取服务列表信息
        """
        try:
            field_info = {field: value}
            order_info = {"classes": "asc", "name": "asc"}
            result = FSRContainerHPA.filter_by_field_search(db, page, per_page, field_info, order_info)
            if not result:
                logger.error(f"get container hpa search, result is null => {field}:{value}")
                return False, None

        except Exception as e:
            message = f"get container hpa search by {field}:{value} error"
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return True, result




    def get_container_hpa_match(self, db: Session, page: int, per_page: int, field: str, value: str):
        """
        根据表字段完全匹配，获取服务列表信息
        """
        try:
            result = FSRContainerHPA.filter_by_field_match(db, page, per_page, {field: value})
            if not result:
                logger.error(f"get container hpa match, result is null => {field}:{value}")
                return False

        except Exception as e:
            message = f"get container hpa match by {field}:{value} error"
            logger.exception(f"{message} => {e}")
            return resp_500(message)

        return result




    def add_container_hpa(self, db: Session, data: dict):
        """
        创建服务信息
        """
        try:
            container_hpa_object = FSRContainerHPA.get_object_by_name(db, data["name"])
            if container_hpa_object:
                # 如果存在， 则异常返回
                logger.error(f"add container hpa failed, name:{data['name']} was exist")
                return False

            result = FSRContainerHPA.add(db, data)
            if not result:
                logger.error(f"add container hpa to database failed")
                return False
        except Exception as e:
            message = f"add container hpa error"
            logger.exception(f"{message} data:{data} => {e}")
            return resp_500(message)

        return True



    def update_container_hpa(self, db: Session, data: dict):
        """
        修改服务信息
        """
        try:
            container_hpa_object = FSRContainerHPA.get_object_by_uuid(db, data['uuid'])
            if not container_hpa_object:
                # 如果不存在， 则异常返回
                logger.error(f"update container hpa failed, uuid:{data['uuid']} not exist")
                return False

            # 更新服务信息
            result = FSRContainerHPA.update(db, data)
            if not result:
                logger.error(f"update container hpa to database failed")
                return False
        except Exception as e:
            message = f"update container hpa error"
            logger.exception(f"{message} data:{data} => {e}")
            return resp_500(message)

        return True



    def delete_container_hpa(self, db: Session, data: dict):
        """
        删除服务信息
        """
        try:
            container_hpa_object = FSRContainerHPA.get_object_by_uuid(db, data['uuid'])
            if not container_hpa_object:
                # 如果不存在， 则异常返回
                logger.error(f"delete container hpa failed, uuid:{data['uuid']} not exist")
                return False

            # 删除服务信息
            FSRContainerHPA.delete_object_by_uuid(db, data['uuid'])
        except Exception as e:
            message = f"delete container hpa error"
            logger.exception(f"{message} data:{data} => {e}")
            return resp_500(message)

        return True



